//
//  BaseHttpService.m
//  Unity-iPhone
//
//  Created by 宁校航 on 15/11/10.
//
//

#import "BaseHttpService.h"
#import "AppDelegate.h"

@implementation BaseHttpService

#pragma mark - 向服务器请求数据
-(void)requestToService:(NSDictionary *)json
{
    NSString *url = [self getUrl];
    
//    NSString *requestName = [self getServiceName];
//    if ([requestName isEqualToString:@"moGetMessage"]) {
//        [TOSConfig sharedInstance].requestMessageNum ++;
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"StartRequestNewMessage" object:self];
//    }
    
    __weak BaseHttpService *weakSelf = self;
    
    [MainInterface postJSONWithMethod:url parameters:json success:^(id responseObject) {
        
        responseData = responseObject;
        
        NSMutableDictionary *hResponse = [responseObject valueForKey:@"ResultData"];
        
        if ([[hResponse valueForKey:@"Result"] integerValue] == 0) {
            id data = [responseObject valueForKey:@"Data"];
            [weakSelf requestSuccessData:data AndRequest:json];
        }
        else if([[hResponse valueForKey:@"Result"] integerValue] == -3)
        {
//            AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//            [appDelegate logoutAlert];
        }
        else
        {
            weakSelf.baseHttpServiceBlock(hResponse);
        }
    } fail:^{
//        weakSelf.baseHttpServiceBlock(hResponse);
        [weakSelf failedMessage:json];
    }];
}

#pragma mark - 获取调用服务的接口名称
-(NSString *)getServiceName
{
    return @"";
}

#pragma mark - 将服务器接口名称拼接为服务器地址
-(NSString *)getUrl
{
    NSString *url;
    
    NSString *serviceName = [self getServiceName];
    
    if([[serviceName substringToIndex:2] isEqualToString:@"mo"])
    {
        url = [NSString stringWithFormat:@"%@/%@",[WellConfig getWebServiceUrl],serviceName];
    }
    else
    {
        url = [NSString stringWithFormat:@"%@/%@",[WellConfig getFileWebServiceUrl],serviceName];
    }
    
    return url;
}

#pragma mark - 请求失败
-(void)failedMessage:(NSDictionary *)send
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setValue:[NSNumber numberWithInteger:-1] forKey:@"Result"];
    [dic setValue:@"网络错误" forKey:@"ErrMsg"];
    self.baseHttpServiceBlock(dic);
}

#pragma mark - 服务器处理失败后，去本地进行查找处理
-(NSString *)dealFailed:(NSDictionary *)send
{
    return @"";
}

#pragma mark - 请求成功后的数据处理，由子类实现
-(void)requestSuccessData:(id)data AndRequest:(NSDictionary *)request
{
    
}

@end
